package com.goktech.olala.client.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.goktech.olala.client.controller.basic.BasicController;
import com.goktech.olala.client.controller.basic.ResultStatus;
import com.goktech.olala.core.config.SysConfig;
import com.goktech.olala.core.req.ReqGoods;
import com.goktech.olala.core.resp.RespGoodsVo;
import com.goktech.olala.core.service.IGoodsService;
import com.goktech.olala.utils.RMBUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
/**
 * @author wusq
 * 商品控制器层
 */
@Controller
@RequestMapping(value = "/goodsApi")
public class GoodsController extends BasicController {

    @Autowired
    IGoodsService goodsService;

    /**
     * 商品查询
     *
     * @param request
     * @param sEcho
     * @param iDisplayStart
     * @param iDisplayLength
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryGoodsList.do")
    @ResponseBody
    public ModelMap queryGoodsList(HttpServletRequest request, Integer sEcho,
                                   Integer iDisplayStart, Integer iDisplayLength) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = SysConfig.SUCCESS_CODE;
        String respMsg = "";

        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            ReqGoods goodsReq = build(params);

            int pageIndex = iDisplayStart / iDisplayLength+1;
            PageInfo<RespGoodsVo> pageInfo = goodsService.queryGoodsByParam(goodsReq, pageIndex, iDisplayLength);
            if (pageInfo != null && CollectionUtils.isEmpty(pageInfo.getList())) {
                respCode = ResultStatus.QUERY_NULL.getCode();
                respMsg = ResultStatus.QUERY_NULL.getMsg();
            }
            respData = super.respParamsFormat(respCode, respMsg, sEcho, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = ResultStatus.BAD_REQUEST.getCode();
            respMsg = ResultStatus.BAD_REQUEST.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 根据主键ID查询商品信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryGoodsInfoById.do")
    @ResponseBody
    public ModelMap queryGoodsInfoById(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = SysConfig.SUCCESS_CODE;
        String respMsg = "";

        JSONObject respData = null;
        try {
            JSONObject params = super.reqParamsFormat(request);
            String goodsId = params.getString("goodsId");

            RespGoodsVo respGoodsVo = goodsService.queryGoodsById(goodsId);
            respData = super.respParamsFormat(respCode, respMsg, respGoodsVo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = ResultStatus.BAD_REQUEST.getCode();
            respMsg = ResultStatus.BAD_REQUEST.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 商品删除
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/removeGoodsById.do", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap removeGoodsById(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = SysConfig.SUCCESS_CODE;
        String respMsg = "删除成功";

        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            String goodsId = params.getString("goodsId");

            int result = goodsService.removeGoodsById(goodsId);
            respData = super.respParamsFormat(respCode,respMsg, result);
        } catch (Exception e) {
            e.printStackTrace();
            respData = super.respParamsFormat(SysConfig.ERROR_CODE,"服务异常", null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 商品上/下架
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateGoodsIsOnById.do", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap updateGoodsIsOnById(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = SysConfig.SUCCESS_CODE;
        String respMsg = "";

        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            String goodsId = params.getString("goodsId");
            Integer isOnSale = params.getInteger("isOnSale");

            int result = goodsService.updateGoodsIsOnById(goodsId, isOnSale);
            respData = super.respParamsFormat(respCode,respMsg, result);
        } catch (Exception e) {
            e.printStackTrace();
            respData = super.respParamsFormat(SysConfig.ERROR_CODE,"服务异常", null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }


    /**
     * 保存商品信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveGoodsInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap saveGoodsInfo(HttpServletRequest request) {

        ModelMap resultMap = new ModelMap();
        Integer respCode = ResultStatus.SUCCESS.getCode();
        String respMsg = ResultStatus.SUCCESS.getMsg();

        JSONObject respData = null;
        try {
            JSONObject params = super.reqParamsFormat(request);
            ReqGoods goodsReq = build(params);

            int result = goodsService.saveGoodsInfo(goodsReq);
            respData = super.respParamsFormat(respCode, respMsg, result);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = ResultStatus.BAD_REQUEST.getCode();
            respMsg = ResultStatus.BAD_REQUEST.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }


    /**
     * 构建查询实体类
     *
     * @param params
     * @return
     */
    private ReqGoods build(JSONObject params) {
        return JSONObject.toJavaObject(params,ReqGoods.class);
    }
}
